package org.cttic.visualization.visualization.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.model.Filters;
import lombok.SneakyThrows;
import org.bson.Document;
import org.cttic.visualization.visualization.common.commonConf;
import org.cttic.visualization.visualization.common.itineraryQueryData;
import org.cttic.visualization.visualization.service.ItineraryQuery;
import org.cttic.visualization.visualization.util.DataUtil;
import org.cttic.visualization.visualization.util.DateUtil;
import org.cttic.visualization.visualization.util.ParseJsonDataUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author shkstart
 * @create 2020-04-29 17:28
 * 接口  2.3.1车辆行程查询接口
 */
@Service
public class ItineraryQueryImpl implements ItineraryQuery {
    @Autowired
    MongoTemplate mongoTemplate;

    @Override
    public String itineraryQuery(String inputData) {
        JSONObject jsonData = ParseJsonDataUtil.getJsonData(inputData);
        String provinceId = jsonData.getString("provinceId");
        String vehPlate = jsonData.getString("vehPlate");
        String vehPlateColor = jsonData.getString("vehPlateColor");
        String queryType = jsonData.getString("queryType");
        String feeFlag = jsonData.getString("feeFlag");
        String queryValue1 = jsonData.getString("queryValue1");
//        List<itineraryQueryData.Trip> list = new ArrayList<>();
//
        itineraryQueryData itineraryQueryData = new itineraryQueryData();
//
//        org.cttic.visualization.visualization.common.itineraryQueryData.Trip trip = new itineraryQueryData.Trip();

        //按行程编码查询
        itineraryQueryData.setPageNo(1);
        itineraryQueryData.setPageSize(1);
        itineraryQueryData.setTotalCount(1);
        itineraryQueryData.setTotalPage(1);
        if(queryType.equals("2")){

            FindIterable<Document> documents = mongoTemplate.getDb().getCollection(commonConf.mongoStrip)
                    .find(Filters.and(Filters.eq("vehicleId", vehPlate)
                            , Filters.eq("vehicleIdColor", Integer.parseInt(vehPlateColor)), Filters.eq("endLat", queryValue1)));
            List<itineraryQueryData.Trip> list = DataUtil.itineraryQueryDataUtil(documents, feeFlag);
//            MongoCursor<Document> iterator = documents.iterator();
//            while (iterator.hasNext()){
//                String json = iterator.next().toJson();
//                JSONObject data = ParseJsonDataUtil.getJsonData(json);
//                String endLat = data.getString("endLat");
//                String vehicleId = data.getString("vehicleId");
//                int vehicleIdColor = data.getIntValue("vehicleIdColor");
//                String entryStation = data.getString("entryStation");
//                String entryStationId = data.getString("entryStationId");
//                String entryTime = data.getString("entryTime");
//                int isOnline = data.getIntValue("isOnline");
//               if(isOnline==1){
//                    String exitTime = data.getString("exitTime");
//                    String exitStationId = data.getString("exitStationId");
//                    String exitStation = data.getString("exitStation");
//                    trip.setExitGantryId(exitStationId);
//                    trip.setExitGantryName(exitStation);
//                    trip.setExitTime(exitTime);
//                }
//                itineraryQueryData.setPageNo(1);
//                itineraryQueryData.setPageSize(1);
//                itineraryQueryData.setTotalCount(1);
//                itineraryQueryData.setTotalPage(1);
//                trip.setEndLat(endLat);
//                trip.setVehPlate(vehicleId);
//                trip.setVehPlateColor(vehicleIdColor+"");
//                trip.setEnGantryName(entryStation);
//                trip.setEnGantryId(entryStationId);
//                trip.setEnTime(entryTime);
//                trip.setIsOnline(isOnline+"");
//
//                trip.setVehStatus("0");
//                //产生一个100以内的整数:int x=(int)(Math.random()*100);
//                if(Integer.parseInt(feeFlag)==1){
//                    trip.setTotalReceivableAmt("***.**");
//                    trip.setTotalDiscountAmt("***.**");
//                }
//                list.add(trip);
                itineraryQueryData.setTripList(list);
            ObjectMapper mapper = new ObjectMapper();
            try {
                String json = mapper.writeValueAsString(itineraryQueryData);
                return json;
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }

            //按时间段查询
        }else if(queryType.equals("1")){
            String queryValue2 = jsonData.getString("queryValue2");     //结束时间
            String startTime = DateUtil.TtoTime(queryValue1);
            String endTime = DateUtil.TtoTime(queryValue2);
            FindIterable<Document> documents = mongoTemplate.getDb().getCollection(commonConf.mongoStrip)
                    .find(Filters.and(Filters.eq("vehicleId", vehPlate),
                            Filters.eq("vehicleIdColor", Integer.parseInt(vehPlateColor)),
                            Filters.lte("entryTime",endTime),
                            Filters.gte("entryTime",startTime)));
            List<org.cttic.visualization.visualization.common.itineraryQueryData.Trip> list = DataUtil.itineraryQueryDataUtil(documents, feeFlag);

            itineraryQueryData.setTripList(list);
            ObjectMapper mapper = new ObjectMapper();
            try {
                String json = mapper.writeValueAsString(itineraryQueryData);
                return json;
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
            //按时间点查询
        }else if(queryType.equals("0")){
            String startTime = DateUtil.TtoTime(queryValue1);
            FindIterable<Document> documents = mongoTemplate.getDb().getCollection(commonConf.mongoStrip)
                    .find(Filters.and(Filters.eq("vehicleId", vehPlate),
                            Filters.eq("vehicleIdColor", Integer.parseInt(vehPlateColor)),
                            Filters.gte("entryTime",startTime)));
            List<org.cttic.visualization.visualization.common.itineraryQueryData.Trip> list = DataUtil.itineraryQueryDataUtil(documents, feeFlag);
            itineraryQueryData.setTripList(list);

            ObjectMapper mapper = new ObjectMapper();
            try {
                String json = mapper.writeValueAsString(itineraryQueryData);
                return json;
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }


        return null;
    }
}
